package com.edu.edu.day9_19_week7_homework.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import com.edu.edu.day9_19_week7_homework.R;
import com.edu.edu.day9_19_week7_homework.SQLite.DBHelpter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 9/19 0019.
 */
public class CollectActivity extends AppCompatActivity{

    private ListView mLv;
    private SQLiteDatabase mDb;
    private DBHelpter dbHelpter;
    private Cursor cursor;
    private SimpleCursorAdapter adapter;
    private List<Map<String, Object>> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);//关联收藏布局
        initView();//初始化控件
        initData();//展示数据库数据
    }

    private void initData() {
        //查询数据库的数据
        cursor = mDb.query("food", new String[]{"_id", "name","url"}, null, null, null, null, null);
//        cursor = mDb.rawQuery("select * from food",null);//构建数据源
        list = new ArrayList<>();
        if(cursor != null){
            while(cursor.moveToNext()){
                Map<String,Object> map = new HashMap<>();
                //遍历数据库,根据列明获得字段号，根据字段号获取对应值
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String url = cursor.getString(cursor.getColumnIndex("url"));
                map.put("name",name);
                map.put("url",url);
                list.add(map);
            }
        }
        assert mLv != null;
        //创建适配器
        adapter = new SimpleCursorAdapter(this, R.layout.collect_list_item, cursor,new String[]{"name","url"},new int[]{R.id.collect_text,R.id.collect_url},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        mLv.setAdapter(adapter);//设置适配器
        //长按删除事件
        mLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
               //根据点击的条目获取集合中对应的值
                String name = (String) list.get(position).get("name");
                //删除字段
                int foodId = mDb.delete("food", "name = ?", new String[]{name});
                if (foodId > 0) {
                    // 重新查询数据库.刷新界面.
                    Cursor newCursor =mDb.rawQuery("select * from food",null);
                    //交换Cursor结果的方法.
                    adapter.swapCursor(newCursor);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(CollectActivity.this, "删除成功!", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
        //短按跳转到详情页面播放视频
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                //点击条目跳转到详情页面
                String caption = (String) list.get(position).get("name");
                String video = (String) list.get(position).get("url");
                //跳转到详情页面，利用意图传网址和标题过去
                Intent intent = new Intent(CollectActivity.this,DetailActivity.class);
                intent.putExtra("caption",caption);
                intent.putExtra("video",video);
                //开启意图
                startActivity(intent);
            }
        });
    }
    //空间初始化
    private void initView() {
        mLv = (ListView) findViewById(R.id.collect_list);
        dbHelpter = new DBHelpter(CollectActivity.this);
        mDb = dbHelpter.getWritableDatabase();
    }
}
